<!-- 配置项编辑弹窗 -->
<template>
  <el-dialog
    title="复制学期"
    :visible="visible"
    width="700px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible">
    <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="100px">

        <el-form-item
          label="复制学期:"
          prop="semester_fz_id">
          <el-select
            filterable
            clearable
            v-model="form.semester_fz_id"
            @change="$forceUpdate()"
            size="small"
            placeholder="-请选择学期-"
            class="ele-block">
            <el-option v-for="item in semesterList" :key="item.id" :label="item.name" :value="item.id"/>
          </el-select>
        </el-form-item>

        <el-form-item
          label="复制到:"
          prop="semester_id">
          <el-select
            filterable
            clearable
            v-model="form.semester_id"
            @change="$forceUpdate()"
            size="small"
            placeholder="-请选择学期-"
            class="ele-block">
            <el-option v-for="item in semesterList" :key="item.id" :label="item.name" :value="item.id"/>
          </el-select>
        </el-form-item>

        <el-form-item
          label="开始日期:"
          prop="start_date">
          <el-date-picker
              v-model="form.start_date"
              type="date"
              placeholder="选择日期">
            </el-date-picker>
        </el-form-item>

        </el-form>
      <div slot="footer">
        <el-button @click="updateVisible(false)">取消</el-button>
        <el-button
          type="primary"
          @click="save"
          :loading="loading">确定
        </el-button>
      </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'ConfigDataEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 类型
    cId: Number,
    stype:Number,

  },
  data() {
    return {
      // 表单数据
      form: Object.assign({}, this.data),
      // 表单验证规则
      rules: {
        semester_fz_id: [
          {required: true, message: '请选择学期', trigger: 'blur'}
        ],
        semester_id: [
          {required: true, message: '请选择学期', trigger: 'blur'}
        ],
        start_date: [
          {required: true, message: '请填写日期', trigger: 'blur'}
        ]
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      semesterList:[],
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
        // 获取学期列表
        this.getSemesterList();
      } else {
        this.form = {status: 1};
        this.isUpdate = false;
      }
    },
    cId() {
      this.getSemesterList();

    },
  },
  mounted() {
    // 获取学期列表
    this.getSemesterList();
  },
  methods: {
    /* 保存编辑 */
    save() {
      console.log(this.cId);
      this.$confirm('确定要复制这一学期数据到当前学期吗?', '提示', {
        type: 'warning'
      }).then(() => {
          const loading = this.$loading({lock: true});
          this.$http.post('/curriculum/copysemester',
          {
            stype:this.stype,
            cId: this.cId,
            semester_fz_id:this.form.semester_fz_id,
            semester_id:this.form.semester_id,
            start_date: this.form.start_date
          })
          .then(res => {
            loading.close();
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              // this.$emit('done');
              // this.reload();
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            loading.close();
            this.$message.error(e.message);
          });
      }).catch(() => {
      });
    },

    /**
     * 获取学期列表
     */
    getSemesterList() {
      if (this.form.semester) {
        // this.form.grade = null;
        this.$forceUpdate();
      }
      // this.$forceUpdate();
      // this.getSemesterList();
      this.$http.post('/semester/getSemesterList', {cid: this.cId}).then(res => {
        if (res.data.code === 0) {
          this.semesterList = res.data.data;

        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    }
  }
}
</script>

<style scoped>
</style>
